<template>
  <div>
    <h1>广告列表</h1>
    <div class="z">
      <el-table :data="ads" stripe style="width: 100%">
        <el-table-column prop="aid" label="ID" width="100" />
        <el-table-column prop="content" label="内容" width="400" />
        <el-table-column prop="time" label="时间" width="300" />
        <el-table-column label="操作">
          <template #default="scope">
            <el-button size="small" type="primary"  @click="handleUpdate(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    

    <!-- 编辑弹出框 -->
    <el-dialog v-model="dialogFormVisible4Edit" title="广告" width="45%" align-center>
      <el-form :model="formData" label-width="120px">
      <el-form-item label="内容">
        <el-input v-model="formData.content" />
      </el-form-item>
      </el-form>
      <el-form-item>
        <el-button type="primary" @click="handleEdit()">修改</el-button>
        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
      </el-form-item>
    </el-dialog>



    <div class="pagination-container">
      <el-pagination
      class="pagination"
      @current-change="handleCurrentChange"
      :current-page="pagination.currentPage"
      :page-size="pagination.pageSize"
      layout="total,prev,pager,next,jumper"
      :total="pagination.total"
      ></el-pagination>
    </div>
  </div>
</template>

<script setup>
import { getApi,postApi,deleteApi,updateApi} from '@/util/api'
</script>

<script>
  export default {
      data() {
        
          return {
              ads: [],
              pagination:{
          currentPage:1,//当前页码
          pageSize:7,//一页多少条
          total:0
        },
        dialogFormVisible4Edit:false,//编辑表单是否可见
              formData:{
                content:''
              },//表单数据
              data:{}
          }
      },
      created(){
        this.getAll();
      }
      ,
      methods:{
        //分页
        getAll(){
          getApi("admin/ads/"+this.pagination.currentPage+"/"+this.pagination.pageSize).then((rs)=>{
              this.pagination.pageSize = rs.data.data.size;
              this.pagination.currentPage = rs.data.data.current;
              this.pagination.total = rs.data.data.total;
              this.ads=rs.data.data.records;
              }).catch(error=>{
                  alert(error);
              })
        },
        handleCurrentChange(currentPage){
          //修改页码值为当前选中页码值
          this.pagination.currentPage = currentPage;
          //执行查询
          this.getAll();
        },
        //删除
        handleDelete(row){
        this.$confirm("此操作永久删除当前数据，是否继续？","提示",{
          type:'info',
          confirmButtonText:"确定",
          cancelButtonText:"取消"
        }).then(()=>{
          deleteApi("admin/ads/"+row.aid).then((res)=>{
        if(res.data.flag){
            
            this.$message.error("删除失败");
        }else{
          this.$message.success("删除成功");
        }
        }).finally(()=>{
          this.getAll();
        });
        }).catch(()=>{
          this.$message.info("取消删除操作");
        });},

          //编辑 弹窗
          handleUpdate(row){
          getApi("admin/ads/"+row.aid).then((res)=>{
            if(res.data.code == 1){
              this.formData = res.data.data;
              this.dialogFormVisible4Edit = true;
              this.data=row
            }else{
              this.$message.error("数据同步失败，自动刷新");
            }
          }).finally(()=>{
            this.getAll();
        })},
                //修改
                handleEdit(){
          updateApi("admin/ads",this.formData).then((res)=>{
            if(res.data.code==1){
              this.dialogFormVisible4Edit = false;
              this.$message.success("修改成功");
            }else{
              this.$message.error("修改失败，请重试");
            }
          }).finally(()=>{
          this.getAll();
        });
          }


      }
      ,
      mounted() {
          
      },
  }
</script>


<style lang="scss" scoped>
.pagination-container{
  position: absolute;
  right: 70px;
  margin-top: 40px;
}
.z{
  margin-top: 30px;
}
</style>